package net.akabana.fchelper
{
    import flash.display.DisplayObject;
    import flash.utils.Dictionary;
    
    import mx.core.IFactory;
    import mx.core.IVisualElement;
    import mx.core.UIComponent;
    import mx.events.FlexEvent;
    
    import spark.components.BorderContainer;
    import spark.components.Group;
    import spark.components.SkinnableContainer;

    public class View extends SkinnableContainer{

        private static const ROOT_GROUP_NAME:String = "";

        private static const GROUP_NAME_DELIMITER:String = "_";

        protected var _accChildMap:Dictionary = new Dictionary();

        public function View(){
            super();
            addEventListener(FlexEvent.CREATION_COMPLETE,onCreationCompleteHandler);
        }

        public function getChildByAccName(accName:String):DisplayObject{
            var result:DisplayObject = null;
            if( _accChildMap.hasOwnProperty(accName)){
                result = _accChildMap[ accName ] as DisplayObject;
            }
            return result;
        }

        private function onCreationCompleteHandler(event:FlexEvent):void{
            removeEventListener(FlexEvent.CREATION_COMPLETE,onCreationCompleteHandler);
            creationCompleted();
        }

        protected override function findSkinParts():void{
            super.findSkinParts();
            if( contentGroup != null ){
                setupAccChildMap(contentGroup);
                for (var id:String in skinParts){
                    //SkinPartが必須の場合は、Skinが定義されているかチェックする
                    if (skinParts[id] == true){
                        if (!( id in _accChildMap )){
                            throw(new Error(resourceManager.getString("components", "requiredSkinPartNotFound", [id])));
                        }
                    }
                    //
                    if ( id in _accChildMap){
                        this[id] = _accChildMap[id];
                        if (this[id] != null && !(this[id] is IFactory)){
                            partAdded(id, this[id]);
                        }
                    }
                }
            }
        }

        protected function setupAccChildMap(group:Group, groupAccName:String=""):void{
            var numElements:int = group.numElements;
            var element:IVisualElement;
            var component:UIComponent;
            var accName:String;
            var childGroupAccName:String;
            for( var i:int = 0; i < numElements; i++ ){
                element = group.getElementAt(i);
                if( element is UIComponent ){
                    component = element as UIComponent;
                    accName = component.accessibilityName;
                    if( accName != null && accName.length > 0 ){
                        _accChildMap[groupAccName+accName] = component;
                    }
                    if( element is Group ){
                        if( component.accessibilityName == null || component.accessibilityName.length == 0 ){
                            childGroupAccName = ROOT_GROUP_NAME;
                        } else {
                            childGroupAccName = component.accessibilityName+GROUP_NAME_DELIMITER;
                        }
                        setupAccChildMap( component as Group, groupAccName + childGroupAccName );
                    }
                }
            }
        }

        protected function creationCompleted():void{
        }
    }
}